import  tensorflow as tf

def focal_loss(y_true, y_pred):
    
    gamma = tf.constant(2, dtype=tf.float32)
    alpha = tf.constant(0.25, dtype=tf.float32)
    y_true = tf.cast(y_true, dtype=tf.float32)
    alpha_t = y_true * alpha + (tf.ones_like(y_true) - y_true) * (1 - alpha)
    p_t = y_true * y_pred + (tf.ones_like(y_true) - y_true) * (tf.ones_like(y_true) - y_pred) + tf.keras.backend.epsilon()
    loss = -alpha_t * tf.math.pow((tf.ones_like(y_true)-p_t), gamma) * tf.math.log(p_t)
    return tf.reduce_mean(loss)

def dice_coefficient(y_true_cls, y_pred_cls):   
    
    intersection = tf.reduce_sum(y_true_cls * y_pred_cls )    
    union = tf.reduce_sum(y_true_cls ) + tf.reduce_sum(y_pred_cls)   
    loss = 1. - (2 * intersection / union)       
    return loss